package com.scommunity.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scommunity.admin.vo.HouseQO;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.House;
import com.scommunity.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/house")
public class HouseController {
    @Autowired
    IHouseService houseService;

    @GetMapping({"/list/{pageNum}"})
    public Object list(HouseQO houseQO, @PathVariable(required = false) Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<House> pageInfo = houseService.findPageInfoByCondition(houseQO, pageNum, 4);
        return ResponseResult.success(pageInfo);
    }

    @GetMapping("/getAll")
    public Object getAll() {
        List<House> houses = houseService.getAll();
        return ResponseResult.success(houses);
    }

    @GetMapping("/getAllByStatus")
    public Object getAllByStatus() {
        List<House> houses = houseService.getAllByStatus();
        return ResponseResult.success(houses);
    }

    @PostMapping("/add")
    public Object add(@RequestBody House house) {
        houseService.add(house);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    public Object update(@RequestBody House house) {
        houseService.update(house);
        return ResponseResult.success();
    }

    @GetMapping("/del")
    public Object del(String id) {
        if (houseService.getStatuseById(id) == 0) {
            houseService.del(id);
            return ResponseResult.success();
        } else {
            return ResponseResult.error(201, "房屋已入住，不能删除");
        }
    }

    @GetMapping("/getOwnerHouses")
    public Object getOwnerHouses(String id) {
        List<House> houses = houseService.getOwnerHouses(id);
        return ResponseResult.success(houses);
    }

}
